package com.hgyc.mom.qms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.qms.entity.QmsIndex;
import com.hgyc.mom.qms.vo.QmsIndexParam;
import com.hgyc.mom.qms.service.QmsIndexService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 检测项Controller
 *
 * @author fwj
 * @since 2025-06-25
 */
@Tag(name = "检测项控制器", description = "检测项控制器")
@RestController
@RequestMapping("/qms/index")
public class QmsIndexController {
    @Autowired
    private QmsIndexService qmsIndexService;

    @Operation(summary = "查询检测项列表", description = "查询检测项列表")
    @PostMapping("/pageList")
    public Result<IPage<QmsIndex>> pageList(@Parameter(description = "查询参数") @RequestBody QmsIndexParam qmsIndexParam) {
        Page<QmsIndex> page = new Page<>(qmsIndexParam.getPage(), qmsIndexParam.getPageSize());
        QmsIndex qmsIndex = new QmsIndex();
        BeanUtils.copyProperties(qmsIndexParam, qmsIndex);
        QueryWrapper<QmsIndex> wrapper = QueryWrapperUtil.build(qmsIndex);
        IPage<QmsIndex> list = qmsIndexService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询检测项列表", description = "查询检测项列表")
    @GetMapping("/list")
    public Result<?> list() {
        return  Result.success(qmsIndexService.list());
    }

    @Operation(summary = "获取检测项详细信息", description = "获取检测项详细信息")
    @GetMapping(value = "/{id}")
    public Result<?> getInfo(@Parameter(description = "检测项ID") @PathVariable("id") Long id) {
        return Result.success(qmsIndexService.getById(id));
    }
    @Operation(summary = "新增检测项", description = "新增检测项")
    @PostMapping
    public Result<?> save(@Parameter(description = "检测项") @RequestBody QmsIndex qmsIndex) {
        qmsIndexService.saveIndex(qmsIndex);
        return Result.success();
    }

    @Operation(summary = "修改检测项", description = "修改检测项")
    @PutMapping
    public Result<?> update(@Parameter(description = "检测项") @RequestBody QmsIndex qmsIndex) {
        qmsIndexService.updateIndex(qmsIndex);
        return Result.success();
    }

    @Operation(summary = "删除检测项", description = "删除检测项")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(qmsIndexService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QmsIndex> util = new ExcelUtil<>(QmsIndex.class);
        util.importTemplateExcel(response, "检测项");
    }

    @Operation(summary = "导出检测项列表", description = "导出检测项列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsIndexParam qmsIndexParam) {
        QmsIndex qmsIndex = new QmsIndex();
        BeanUtils.copyProperties(qmsIndexParam, qmsIndex);
        QueryWrapper<QmsIndex> wrapper = QueryWrapperUtil.build(qmsIndex);
        List<QmsIndex> list = qmsIndexService.list(wrapper);
        ExcelUtil<QmsIndex> util = new ExcelUtil<>(QmsIndex.class);
        util.exportExcel(response, list, "检测项数据");
    }

    @Operation(summary = "导入检测项列表", description = "导入检测项列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QmsIndex> util = new ExcelUtil<>(QmsIndex.class);
        List<QmsIndex> qmsIndexList = util.importExcel(file.getInputStream());
        String message = qmsIndexService.importData(qmsIndexList, updateSupport);
        return Result.success(message);
    }
}